package com.g24.partychecker;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class Info extends Activity
{
	
	WebDBAdapter webdb;
	DBAdapter mDbHelper;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);
		
		mDbHelper = new DBAdapter(this);
        mDbHelper.open();
		
		Intent i = getIntent();
		Bundle b = i.getExtras();
		String newID = b.getString("FEESTID");
		
		try 
		{
			webdb = new WebDBAdapter();
			String a = webdb.getDataInfo(newID);
			
			//parse json data
		    JSONArray jArray = new JSONArray(a);
		    String abc = null;
		    String def = null;
		    String ghi = null;
		    for(int i1=0;i1<jArray.length();i1++)
		    {
		    	JSONObject json_data = jArray.getJSONObject(i1);	    	
		    	abc = " Naam:  " + json_data.getString("naam"); 
		    	def = "   " + json_data.getString("subnaam")
		    			+ "\n Genre:  " + json_data.getString("genre")
		    			+ "\n Datum:  " + json_data.getString("datum")
		    			+ "\n Tijd:  " + json_data.getString("tijd")
		    			+ "\n Adres:  " + json_data.getString("adres")
		    			+ "\n Plaats  " + json_data.getString("plaats");
		    	ghi = "Info:\n" + json_data.getString("info");
		    }
		    
			TextView txtv1 = (TextView) findViewById(R.id.finfo_txt1);
			txtv1.setText(abc);
			TextView txtv2 = (TextView) findViewById(R.id.finfo_txt2);
			txtv2.setText(def);
			TextView txtv3 = (TextView) findViewById(R.id.finfo_txt3);
			txtv3.setText(ghi);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	private int MENU_FEEST_FAVORIET;
	private String naam;
	private String subnaam;
	private String genre;
	private String datum;
	private String tijd;
	private String adres;
	private String plaats;
	private String regio;
	private String website;
	private String info;
	private String maplat;
	private String maplon;

	/* Creates the menu items */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, MENU_FEEST_FAVORIET, 0, "Toevoegen als Favoriet").setIcon(R.drawable.ic_menu_upload);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	if(item.getItemId() == MENU_FEEST_FAVORIET) 
        {
    		Intent i = getIntent();
    		Bundle b = i.getExtras();
    		String newID = b.getString("FEESTID");
    		
    		try 
    		{
    			webdb = new WebDBAdapter();
    			String a = webdb.getDataInfo(newID);
    			
    			//parse json data
    		    JSONArray jArray = new JSONArray(a);
    		    for(int i1=0;i1<jArray.length();i1++)
    		    {
    		    	JSONObject json_data = jArray.getJSONObject(i1);	    	
    		    	naam = json_data.getString("naam"); 
    		    	subnaam = json_data.getString("subnaam");
    		    	genre = json_data.getString("genre");
    		    	datum = json_data.getString("datum");
    		    	tijd = json_data.getString("tijd");
    		    	adres = json_data.getString("adres");
    		    	plaats = json_data.getString("plaats");
    		    	regio = json_data.getString("regio");
    		    	website = json_data.getString("website");
    		    	info = json_data.getString("info");
    		    	maplat = json_data.getString("maplat");
    		    	maplon = json_data.getString("maplon");
    		    }
    		    mDbHelper.createEvent(naam, subnaam, genre, datum, tijd, adres, plaats, regio, website, info, maplat, maplon);
    		} 
    		catch (Exception e) 
    		{
    			e.printStackTrace();
    		}
    		
    		Context context = getApplicationContext();
     	   	CharSequence text = naam + " Opgeslagen";
     	   	int duration = Toast.LENGTH_SHORT;
     	   	Toast toast = Toast.makeText(context, text, duration);
     	   	toast.show();
     	   	Toast.makeText(context, text, duration).show();
			return true;
        }
        return false;
    }
}